package com.night.attendance.serviceImpl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.night.attendance.feign.StudentFeign;
import com.night.attendance.mapper.VacationMapper;
import com.night.attendance.service.VacationService;
import com.night.common.entity.PageBean;
import com.night.common.entity.ResultVo;
import com.night.common.entity.administration.Teacher;
import com.night.common.entity.attendance.VacationDO;
import com.night.common.entity.attendance.VacationQueryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dark-Morning
 * 2022/10/20 9:34
 */
@Service
@SuppressWarnings("all")
public class VacationServiceImpl implements VacationService {
    @Autowired
    VacationMapper vacationMapper;

    @Autowired
    StudentFeign studentFeign;

    public void save(VacationDO vacationDO){
        ResultVo<Teacher> teacherResultVo = studentFeign.getCourseByStudent(vacationDO.getStudentId());
        Teacher teacher = teacherResultVo.getData();
        vacationDO.setCounsellor(teacher.getName());
        vacationDO.setTeacherId(teacher.getId());
        vacationDO.setState("0");
        vacationDO.setApplyTime(DateUtil.now());
        vacationMapper.insertOne(vacationDO);
    }

    public void update(VacationDO vacationDO){
        vacationMapper.updateById(vacationDO);
    }

    public void listVacation(VacationQueryDO vacationQueryDO){
        PageBean<List<VacationDO>> pageBean = vacationQueryDO.getPageBean();
        Page<VacationDO> p  = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSize());
        List<VacationDO> vacationDOList = vacationMapper.selectByCondition(vacationQueryDO);
        pageBean.setData(vacationDOList);
        pageBean.setTotalNum((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
    }
}
